$(function () {
    var edit_dialog =  $('#edit_dialog');
    var edit_form =  $('#edit_form');
    var employee_datagrid =  $('#employee_datagrid');
    var role_combobox = $('#role_combobox');

    //数据表格
    $('#employee_datagrid').datagrid({
        url:'/employee/list.do',
        striped:true,
        singleSelect:true,
        fitColumns:true,
        pagination:true,
        rownumbers:true,
        toolbar:"#tb",
        columns:[[
            {field:'id',hidden:true,width:100},
            {field:'cb',checkbox:true},
            {field:'username',title:'用户名称',width:100},
            {field:'realname',title:'真实姓名',width:100,align:'right'},
            {field:'password',title:'密码',width:100,align:'right'},
            {field:'tel',title:'联系方式',width:100,align:'right'},
            {field:'email',title:'邮箱',width:100,align:'right'},
            {field:'dept',title:'所属部门',width:100,align:'right',formatter:deptFormatter},
            {field:'inputTime',title:'入职日期',width:100,align:'right'},
            {field:'state',title:'状态',width:100,align:'right',formatter:stateFormatter},
            {field:'admin',title:'是否为管理员',width:100,align:'right',formatter:adminFormatter}
        ]]
    });
    //end



    var cmdObj = {
        //点击添加按钮,添加员工
        add : function () {
            edit_dialog.dialog("setTitle","添加员工");
            edit_form.form("clear");
            edit_dialog.dialog("open");
        },
        //刷新
        reload : function () {
            employee_datagrid.datagrid("reload");
        },
        //编辑按钮编辑员工
        edit : function () {

            var row = employee_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要编辑的员工","info");
                return;
            }

            edit_dialog.dialog("setTitle","编辑员工");

            //在读取数据之前,清除缓存
            edit_form.form("clear");


            //发起请求获取角色的ids,设置到角色的下拉框中
            $.get("/role/getRoleIdsByEmployeeId.do?id="+row.id,function (data) {
                role_combobox.combobox('setValues',data);

            })

            if (!row.dept){
                edit_form.form("load",row);
                edit_dialog.dialog("open");
                return;
            }

            row["dept.id"] = row.dept.id;

            edit_form.form("load",row);



            edit_dialog.dialog("open");
        },
        //save按钮提交表单
        save : function () {

            var url = "";

            var id = $("#employeeId").val();

            if(id){
                url = "/employee/update.do";
            }else{
                url = "/employee/save.do";
            }

            edit_form.form('submit', {
                url:url,
                onSubmit : function(param){
                    var ids = $('#role_combobox').combobox('getValues');
                    for (var i = 0; i < ids.length; i++){
                        param["roles["+ i +"].id"] = ids[i];
                    }
                },
                success: function(data){
                    var data = eval('(' + data + ')');
                    if (data.success){
                        $.messager.confirm('温馨提示','操作成功',function(r){
                            edit_dialog.dialog("close");
                            employee_datagrid.datagrid("load");
                        });
                    }else{
                        $.messager.alert("温馨提示",data.msg);
                    }
                }
            });
        },
        //删除员工
        remove : function () {
            var row = employee_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择要删除的员工","info");
                return;
            }

            $.messager.confirm('确认','您确认想要删除记录吗？',function(r){
                if (r){
                    $.get("/employee/delete.do?id=" + row.id,function (data) {
                        if (data.success){
                            $.messager.confirm('温馨提示','删除成功',function(r){
                                employee_datagrid.datagrid("load");
                            });
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },
        //离职与复职操作
        changeState : function () {

            var row = employee_datagrid.datagrid("getSelected");
            if (!row){
                $.messager.alert("温馨提示","请选择操作的员工","info");
                return;
            }

            $.messager.confirm('确认','您确认要修改该员工的状态吗？',function(r){
                if (r){
                    $.get("/employee/changeState.do?id=" + row.id,function (data) {
                        if (data.success){
                            $.messager.confirm('温馨提示','改变成功',function(r){
                                employee_datagrid.datagrid("load");
                            });
                        }else{
                            $.messager.alert("温馨提示",data.msg,"error");
                        }
                    })
                }
            });
        },
        //编辑的关闭按钮功能
        cancel : function () {
            edit_dialog.dialog("close");
        }


    }

    //抽取优化
    $("a[data-cmd]").click(function () {
        var cmd = $(this).data("cmd");

        cmdObj[cmd]();
    })
    //end

    //dialog弹窗
    $('#edit_dialog').dialog({
        title: 'My Dialog',
        buttons:"#bb",
        closed:true,
        width: 350,
        height: 400,
    });
    //end


    $('#employee_datagrid').datagrid({
        //根据点击的行中状态的信息判断按钮显示离职还是在职
        onClickRow: function(index,row){

            if (row.state){
                $("#changeState").html("复职");
            }else{
                $("#changeState").html("离职");
            }
        }
    });
    //end

})//end




//部门格式化
function deptFormatter(value,row,index) {
    if (value != null){
        return value.name;
    }
}
//状态格式化
function stateFormatter(value,row,index) {
    if (value == 0){
        return "<span style='color: green'>在职</span>"
    } else{
        return "<span style='color: red'>离职</span>"
    }
}
//管理员格式化
function adminFormatter(value,row,index) {
    return value ? "是" : "否";
}




function query() {
    var param = $("#keyword").val();

    $('#employee_datagrid').datagrid('load',{
        keyword: param,
    });


}
